package com.eascs.admin.sys.organ;

import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.eascs.admin.sys.organ.form.OrgForm;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.entity.sys.ActOrgUser;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.vo.ModelResult;

@RestController
@RequestMapping("/organ/rest")
public class ActOrganRestController extends BaseRestController {
	@Reference
	private SysOrgService sysOrgService;
	
	@RequestMapping(value = "/allList", method = RequestMethod.GET)
	@ResponseBody
	public BaseRestResult<String> getOrgAllList(){
    	List<ActOrgEntity> list = sysOrgService.getOrgAllList();
    	String json=JSON.toJSONString(list);
		return BaseRestResult.buildRestResult(json);
	}

	@RequestMapping(value = "/getOrgByIds", method = RequestMethod.POST)
	public BaseRestResult<String> getOrgByIds(String ids) {
		ActOrgEntity orgEntity = sysOrgService.getOrgByIds(ids);
		if (orgEntity == null) {
			ViewContext.put("reso", orgEntity);
			return BaseRestResult.refresh();
		} else {
			return BaseRestResult.warn("查询失败!");
		}
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public BaseRestResult<String> save(OrgForm form) {
		ActOrgEntity orgEntity = new ActOrgEntity();
		try {
			BeanUtils.copyProperties(orgEntity, form);
		} catch (Exception e) {}
		User user=UserHolder.getUser();
		orgEntity.setUpdateTime(new Date());
		orgEntity.setCreateTime(new Date());
		orgEntity.setUpdatorNo(user.getNo());
		orgEntity.setCreatorNo(user.getNo());
		ActOrgEntity result = sysOrgService.insert(orgEntity);
		if (result.getOrgId() != null) {
			return BaseRestResult.buildRestResult(JSON.toJSONString(result));
		} else {
			return BaseRestResult.warn("新增失败!");
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public BaseRestResult<String> update(OrgForm form) {
		ActOrgEntity orgEntity = new ActOrgEntity();
		try {
			BeanUtils.copyProperties(orgEntity, form);
		} catch (Exception e) {}
		User user=UserHolder.getUser();
		orgEntity.setUpdateTime(new Date());
		orgEntity.setUpdatorNo(user.getNo());
		Integer result = sysOrgService.update(orgEntity);
		if (result > 0) {
			return BaseRestResult.buildSuccessResult();
		} else {
			return BaseRestResult.warn("修改失败!");
		}
	}
	
	
	@RequestMapping(value = "/updateParentId", method = RequestMethod.POST)
	public BaseRestResult<String> updateParentId(String orgId,String parentId) {
		ActOrgEntity orgEntity =  sysOrgService.getOrgByIds(orgId);
		orgEntity.setParentId(parentId);
		User user=UserHolder.getUser();
		orgEntity.setUpdateTime(new Date());
		orgEntity.setUpdatorNo(user.getNo());
		
		Integer result = sysOrgService.update(orgEntity);
		if (result > 0) {
			return BaseRestResult.buildSuccessResult();
		} else {
			return BaseRestResult.warn("修改失败!");
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public BaseRestResult<String> delete(String orgId, String ftlFlag) {
		//ftlFlag为页面标记，当ftlFlag为organInfo时，对删除进行权限操作
		if ("organInfo".equals(ftlFlag)) {
	    	User user=UserHolder.getUser();
	    	if (!user.isSuperAdministrator()) {
	    		return BaseRestResult.warn("您没有删除权限!");
	    	}
		}
		Integer result = sysOrgService.delete(orgId);
		if (result > 0) {
			return BaseRestResult.buildSuccessResult();
		} else {
			return BaseRestResult.warn("删除失败!");
		}
	}
	
	@RequestMapping(value="/add/employeePopup")
	public HtmlRestResult editBasic(){
		return  HtmlRestResult.popHtmlFragment("sys/addEmployeePopup");
	}
	
	@RequestMapping(value = "/save/roleUser", method = RequestMethod.POST)
	public BaseRestResult<String> saveRoleUser(String orgId,String userNo) {
		if(StringUtils.isBlank(orgId) || StringUtils.isBlank(userNo)){
			return BaseRestResult.warn("新增失败,参数不能为空!");
		}
		ActOrgEntity org=sysOrgService.getOrgByIds(orgId);
		if((StringUtils.isBlank(org.getUnitCode()) || org.getUnitCode() == null) 
				&& org.getOrgLevel()== ActOrgEntity.ORG_LEVEL_PLATFORM){
			return BaseRestResult.warn("新增失败,EA经营单位编号为空!");
		}
		ModelResult<ActOrgEntity> modelResult=sysOrgService.getActOrgByUserNo(userNo);
		if(modelResult.isSuccess()){
			ActOrgEntity  actOrgEntity=modelResult.getModel();
			if(actOrgEntity != null){
				return BaseRestResult.warn("新增失败,该用户已存在，不能重复添加!");
			}
			
		}
		ActOrgUser actOrgUser = new ActOrgUser();
		User user=UserHolder.getUser();
		actOrgUser.setUpdateTime(new Date());
		actOrgUser.setCreateTime(new Date());
		actOrgUser.setUpdatorNo(user.getNo());
		actOrgUser.setCreatorNo(user.getNo());
		actOrgUser.setOrgId(orgId);
		actOrgUser.setUserNo(userNo);
		ModelResult<Integer>  result=sysOrgService.insertActOrgUser(actOrgUser);
		if (result.isSuccess()) {
			return BaseRestResult.buildRestResult(JSON.toJSONString(result));
		} else {
			return BaseRestResult.warn("新增失败!");
		}
	}
	
	
	@RequestMapping(value = "/delete/roleUser", method = RequestMethod.GET)
	public BaseRestResult<String> deleteRoleUser(String id,String userNo) {
		if(StringUtils.isBlank(id) || StringUtils.isBlank(userNo)){
			return BaseRestResult.warn("删除失败,参数不能为空!");
		}
		ModelResult<Integer>  result=sysOrgService.deleteActOrgUser(id,userNo);
		if (result.isSuccess()) {
			return BaseRestResult.buildRestResult(JSON.toJSONString(result));
		} else {
			return BaseRestResult.warn("删除失败!");
		}
	}

}
